#include <iostream>
#include <vector>
#include <string>
using namespace std;
long calOrder(string &s) {
	long r = 0;
	long fac = 1;
	int z = 1;
	for (int i = s.size() - 2; i >= 0; i--) {
		fac *= z++;
		int count = 0;
		for (int j = i + 1; j < s.size(); j++) {
			if (s[i] > s[j]) {
				count++;
			}
		}
		r += fac * count;
	}
	return r + 1;
}

int maint2() {
	int n;
	cin >> n;
	string s;
	while (cin >> s) {
		cout << calOrder(s) << endl;
	}
	return 0;

}